package Server;

import module.Message;
import java.io.IOException;

/**
 * @author 长风无言
 */
public class ServerReceive {

    private ServerInitClose server;
    private ServerDao sd;


    public ServerReceive(ServerInitClose server) {
        this.server = server;
    }

    public void serverReceive() throws IOException, ClassNotFoundException {

        sd = new ServerDao();
        while (true) {
            System.out.println("等待账号登录");
            Message infom = (Message) server.getIn().readObject();
            System.out.println("接收到的消息是：" + infom);
            if ("managerCheck".equals(infom.getCheckFlag())) {
                if (sd.serverManagerCheck(infom.getUser())) {
                    infom.setCheckFlag("success");
                    System.out.println("管理员账号登录成功！");
                    server.getOut().writeObject(infom);
                    System.out.println("发送完毕！");
                    System.out.println("等待接收");
                    while (true) {
                        String str = (String) server.getIn().readObject();
                        if ("studentmanage".equals(str)) {
                            System.out.println("学生管理");
                            ServerManageStudent.work(server);
                        } else if ("questionmanage".equals(str)) {
                            System.out.println("考题管理");
                            ServerManageQuestions.work(server);
                        }else {
                            return;
                        }
                    }
                }else {
                    infom.setCheckFlag("fail");
                    server.getOut().writeObject(infom);
                    System.out.println("管理员账号登陆失败！");
                }
            }else if("studentcheck".equals(infom.getCheckFlag())){
                if (sd.serverstudentrCheck(infom.getUser())) {
                    infom.setCheckFlag("success");
                    System.out.println("学员账号登录成功！");
                    server.getOut().writeObject(infom);
                    System.out.println("发送完毕！");
                    System.out.println("等待接收");
                    while (true) {
                        String str = (String) server.getIn().readObject();
                        if ("modify".equals(str)) {
                            System.out.println("修改密码");
                            sd.modifyPassword(server);
                        } else if ("exam".equals(str)) {
                            System.out.println("考题管理");
                            ServerExam se = new ServerExam();
                            se.serverExam(server);
                        } else {
                            return;
                        }
                    }
                }else {
                    infom.setCheckFlag("fail");
                    server.getOut().writeObject(infom);
                    System.out.println("管理员账号登陆失败！");
                }
            }
        }
    }
}
